﻿/******************** NG.Respate(C)  **********************************
* 产品名称: Respate 科技
* 文件名称: main.c
* 程序作者: 雷帕特公司(南宫子萍)
* 程序版本: V1.1														   
* 编制日期: 2019/10/10
* 功能描述: 线性表链式结构
* QQ:		2085827949
**********************************************************************/
#include "LinkList1.h"



//创建链表
LinkList* LinkList_Create()
{
	LinkList* list = NULL;
	list = (LinkList*)malloc(sizeof(LinkList));
	if (list == NULL) return NULL;

	memset(list, 0, sizeof(LinkList));
	list->Lenlength = -1;
	list->head = NULL;
	return list;
}

//删除链表
void LinkList_Destroy(LinkList* list)
{
	if (list == NULL) return;
	free(list);
}

//清空链表
void LinkList_Clear(LinkList* list)
{
	if (list == NULL) return;
	memset(list, 0, sizeof(LinkList));
	list->Lenlength = -1;
	list->head = NULL;
}


//链表长度
int LinkList_Length(LinkList* list)
{
	return list->Lenlength;
}


//插入节点
int LinkList_Insert(LinkList* list, LinkListNode* node, int index)
{
	int i = 0;
	LinkListNode* current;
	
	if (list == NULL || node == NULL || index<0) return -1;
	if (index>list->Lenlength)index = list->Lenlength;	//容错

	current = (LinkListNode*)&list->head;//注意
	for (i = 0; i < index&&current->next; i++)
		current = (LinkListNode*)current->next;

	node->next = current->next; 
	current->next = (LinkListNode*)node;

	list->Lenlength++;
	return index;
}


//得到节点
LinkListNode* LinkList_Get(LinkList* list, int index)
{
	int i = 0;
	LinkListNode* current =NULL;

	if (list == NULL || index < 0) return NULL;
	if (index > list->Lenlength) return NULL;

	current = (LinkListNode*)&list->head;
	for (i = 0; i < index&&current->next; i++)
		current = (LinkListNode*)current->next;
	
	return current->next;
}


//删除节点
LinkListNode* LinkList_Delete(LinkList* list, int index)
{
	LinkListNode* current = NULL;
	LinkListNode* node = NULL;

	if (list == NULL || index < 0) return NULL;
	if (index > list->Lenlength) return NULL;

	current = (LinkListNode*)list->head;
	while (current->next&&index-->0)
		current = (LinkListNode*)current->next;

	node = (LinkListNode*)current->next;
	current = (LinkListNode*)node->next;
	list->Lenlength--;
	return node;
}

